﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using VOL;
using System.Data;
using DAL;

namespace BLL
{
    public class DMHangBL
    {
        private DataAccess dataAccess;
        public DMHangBL()
        {
            dataAccess = new DataAccess();
        }

        public DataTable GetData()
        {
            string strSql = "SELECT * FROM tbl_DMHang ORDER BY MaHang DESC ";
            return dataAccess.GetData(strSql);
        }

        public DataTable GetData(DMHang obj, int limit = 99999, float DonGiaMin = 0, float DonGiaMax = 0)
        {
            string strWhere = " WHERE 1=1 ";
            List<SqlParameter> lstParam = new List<SqlParameter>();
            if (obj.MaNhaCungCap > 0)
            {
                lstParam.Add(new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap });
                strWhere += " AND MaNhaCungCap = @MaNhaCungCap ";
            }
            if (obj.MaLoaiHang > 0)
            {
                lstParam.Add(new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang });
                strWhere += " AND MaLoaiHang = @MaLoaiHang ";
            }
            if (DonGiaMin > 0 && DonGiaMax > 0)
            {
                // từ 1m - 2m
                lstParam.Add(new SqlParameter("@DonGiaMin", SqlDbType.NVarChar) { Value = DonGiaMin });
                lstParam.Add(new SqlParameter("@DonGiaMax", SqlDbType.NVarChar) { Value = DonGiaMax });
                strWhere += " AND DonGia BETWEEN @DonGiaMin AND @DonGiaMax ";
            }

            string strLimit = " TOP(" + limit + ") ";
            string strSql = "SELECT " + strLimit + "  * FROM tbl_DMHang " + strWhere + " ORDER BY MaHang DESC ";
            return dataAccess.GetData(strSql, lstParam);
        }

        //Thêm
        public bool insertDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@TenHang", SqlDbType.NVarChar) { Value = obj.TenHang };
            param[1] = new SqlParameter("@HinhAnh", SqlDbType.NVarChar) { Value = obj.HinhAnh };
            param[2] = new SqlParameter("@DVT", SqlDbType.VarChar) { Value = obj.DVT };
            param[3] = new SqlParameter("@DonGia", SqlDbType.NVarChar) { Value = obj.DonGia };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };
            param[5] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };
            param[6] = new SqlParameter("@MoTa", SqlDbType.NVarChar) { Value = obj.MoTa };

            string strSql = "INSERT INTO tbl_DMHang(TenHang,HinhAnh,DVT,DonGia,MaNhaCungCap,MaLoaiHang,MoTa) VALUES (@TenHang,@HinhAnh,@DVT,@DonGia,@MaNhaCungCap,@MaLoaiHang,@MoTa)";
            dataAccess.Execute(strSql, param);

            return true;
        }

        //Xóa
        public bool deleteDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };

            string strSql = "DELETE FROM tbl_DMHang WHERE MaHang = @MaHang";
            dataAccess.Execute(strSql, param);
            return true;
        }

        //Sửa
        public bool updateDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[8];
            param[0] = new SqlParameter("@TenHang", SqlDbType.NVarChar) { Value = obj.TenHang };
            param[1] = new SqlParameter("@HinhAnh", SqlDbType.NVarChar) { Value = obj.HinhAnh };
            param[2] = new SqlParameter("@DVT", SqlDbType.VarChar) { Value = obj.DVT };
            param[3] = new SqlParameter("@DonGia", SqlDbType.NVarChar) { Value = obj.DonGia };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };
            param[5] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };
            param[6] = new SqlParameter("@MoTa", SqlDbType.NVarChar) { Value = obj.MoTa };
            param[7] = new SqlParameter("@MaHang", SqlDbType.NVarChar) { Value = obj.MaHang };

            string strSql = "UPDATE tbl_DMHang SET " +
                            "TenHang = @TenHang, " +
                            "HinhAnh = @HinhAnh, " +
                            "DVT = @DVT, " +
                            "DonGia = @DonGia, " +
                            "MaNhaCungCap = @MaNhaCungCap, " +
                            "MaLoaiHang = @MaLoaiHang, " +
                            "MoTa = @MoTa " +
                            "WHERE MaHang = @MaHang";
            dataAccess.Execute(strSql, param);
            return true;
        }

        //Tìm
        public DataTable searchIDDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };

            string strSql = "SELECT * FROM tbl_DMHang WHERE MaHang = @MaHang";
            return dataAccess.GetData(strSql, param);
        }

        public DataTable Detail(int MaHang)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = MaHang };

            string strSql = "SELECT tbl_DMHang.*, tbl_NhaCungCap.TenNhaCungCap, tbl_LoaiHang.TenLoaiHang FROM tbl_DMHang INNER JOIN " +
                         " tbl_NhaCungCap ON tbl_DMHang.MaNhaCungCap = tbl_NhaCungCap.MaNhaCungCap INNER JOIN " +
                         " tbl_LoaiHang ON tbl_DMHang.MaLoaiHang = tbl_LoaiHang.MaLoaiHang WHERE MaHang = @MaHang";
            return dataAccess.GetData(strSql, param);
        }
    }
}
